﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RoadTeam.SysInterface;
using RoadTeam.Model;
using System.Data;
using RoadTeam.Enums;
using RoadTeam.Pub;
using System.Collections.Specialized;

namespace RoadTeam.BLL
{
    public class RmUnitBll
    {
        private static object o = new object();
        public int ReFlashUnitIdentifierData()
        {
            DataTable dt_Oragan = FactoryClass.CreateSmOrgan().GetALLOraganTable();
            NameValueCollection values = new NameValueCollection();
            foreach (DataRow dr_organ in dt_Oragan.Rows)
            {
                values.Add(dr_organ[0].ToString(), dr_organ[1].ToString());
            }
            DataTable dt_Unit = GetEmptyTable();
            DataRow dr_Unit;
            lock (o)
            {
                DataTable dt = FactoryClass.CreateRmUnit().GetUnitTable();
                foreach (DataRow dr in dt.Rows)
                {
                    dr_Unit = dt_Unit.NewRow();
                    dr[4] = CreateIdentifier.GetIdentifier();
                    dr_Unit["UnitIdentifier"] = dr[4];
                    dr_Unit["UnitName"] = dr[2];
                    dr_Unit["UnitCode"] = dr[1];
                    dr_Unit["UnitFullName"] = dr[3];
                    // dr_Unit["OrganIdentifier"] = FactoryClass.CreateSmOrgan().GetOraganIdentifier(dr[5].ToString());
                    dr_Unit["OrganIdentifier"] = values.GetValues(dr[5].ToString())[0];
                    dt_Unit.Rows.Add(dr_Unit);
                }
                FactoryClass.CreateRmUnit().UpdateUnitTable(dt);
            }
            RmUnit.InsertDatatable(dt_Unit, GlobalConfig.ConnectionString);
            return dt_Unit.Rows.Count;
        }
        public void UpdateUnitData()
        {
        }
        public DataTable GetUnitTable(String OrganIdentifier)
        {
            ReflashUnitDoorType(OrganIdentifier);
            SqlWhere where = new SqlWhere();
            where.Info = new RmUnit() { OrganIdentifier = OrganIdentifier };
            return RmUnit.Select(where, GlobalConfig.ConnectionString);
        }
        private void ReflashUnitDoorType(string OrganIdentifier)
        {
            DataTable dt_DoorType = FactoryClass.CreateCsDoorType().GetALLDoorTypeTable();
            NameValueCollection values = new NameValueCollection();
            foreach (DataRow dr_Door in dt_DoorType.Rows)
            {
                values.Add(dr_Door[0].ToString(), dr_Door[1].ToString());
            }

            String OrganID = FactoryClass.CreateSmOrgan().GetOraganID(OrganIdentifier);
            lock (o)
            {
                DataTable dt_Unit = RmUnit.Select(new SqlWhere()
                {
                    Info = new RmUnit { OrganIdentifier = OrganIdentifier },
                    SelectColumn = @" UnitIdentifier,OrganIdentifier,DoorTypeIdentifier"
                }, GlobalConfig.ConnectionString);
                DataRow dr_Unit;
                DataRow[] dr_Units;
                DataTable dt = FactoryClass.CreateRmUnit().GetUnitTable(OrganID);

                foreach (DataRow dr in dt.Rows)
                {
                    if (dr[1] == null || String.IsNullOrEmpty(dr[1].ToString())) continue;
                    dr_Units = dt_Unit.Select(@"UnitIdentifier='" + dr[2].ToString() + "'");

                    if (dr_Units.Count() == 0) continue;
                    dr_Unit = dr_Units[0];
                    dr_Unit["DoorTypeIdentifier"] = values.GetValues(dr[1].ToString())[0];
                }
                //  dt_Unit.AcceptChanges();
                RmUnit.Update(dt_Unit, GlobalConfig.ConnectionString);
            }
        }
        public DataTable GetEmptyTable()
        {
            SqlWhere where = new SqlWhere();
            where.SelectColumn = @" TOP 0  UnitIdentifier,UnitName,UnitCode,UnitFullName,OrganIdentifier ";
            return RmUnit.Select(where, GlobalConfig.ConnectionString);
        }

        public int GetUnitAllPageCount(String OrganIdentifier)
        {
            RmUnit unit = new RmUnit();
            OQLWhere where = new OQLWhere(unit).AND(unit.OrganIdentifier, "=", OrganIdentifier);
            DataTable dt = RmUnit.Select(where, String.Empty, GlobalConfig.ConnectionString);
            if (dt.Rows.Count == 0) return 0;
            return Convert.ToInt32(dt.Rows[0][0]);
        }

        public DataTable GetUnitDataByPage(string OrganIdentifier, int PageSize, int CurrentPage)
        {
            RmUnit unit = new RmUnit();
            QueryPage page = new QueryPage();
            page.CurrentPage = CurrentPage;
            page.PageSize = PageSize;
            page.OrderByColumn = @"UnitCode";
            page.Where = new OQLWhere(unit).AND(unit.OrganIdentifier, "=", OrganIdentifier);
            return  RmUnit.Select(page, GlobalConfig.ConnectionString);
        }
    }
}
